from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep

# 指定chromedriver路径（137版本）
driver_path = "D:\\pycharm\\PM_test\\chromedriver.exe"

# 创建WebDriver实例
service = Service(driver_path)
options = Options()
browser = webdriver.Chrome(service=service, options=options)
browser.maximize_window()
wait = WebDriverWait(browser, 10)

# 登录函数
def login():
    browser.get("http://146.56.217.9:9131")
    browser.find_element(By.ID, "username").send_keys("admin_9131")
    browser.find_element(By.ID, "password").send_keys("admin")
    browser.find_element(By.CLASS_NAME, "button").click()

# 封装添加盘点函数
def add_inventory(inventory_data):
    # 跳转至新建盘点界面
    browser.find_element(By.ID, "mainmenutd_products").click()
    browser.find_element(By.CSS_SELECTOR, ".menu_titre>a[title='盘点']").click()
    browser.find_element(By.CSS_SELECTOR, ".menu_contenu_product_inventory_card .vsmenu").click()

    # 编号
    browser.find_element(By.ID, "ref").send_keys(inventory_data["ref"])
    # 标签
    browser.find_element(By.ID, "title").send_keys(inventory_data["title"])
    # 仓库
    browser.find_element(By.CSS_SELECTOR, f"select#fk_warehouse>option:nth-child({inventory_data['fk_warehouse']})").click()
    # 产品
    browser.find_element(By.CSS_SELECTOR, f"select#fk_product>option:nth-child({inventory_data['fk_product']})").click()
    # 创建
    browser.find_element(By.NAME, "add").click()

if __name__ == '__main__':
    try:
        login()
        sleep(1)  # 等待页面加载

        # 盘点添加功能测试数据（3个正向用例）
        test_data = [
            {
                "case_desc": "添加标准仓库盘点",
                "ref": "INV-20250615-001",
                "title": "原材料仓库盘点-202506",
                "fk_warehouse": 2,  # 第二个仓库（原材料仓）
                "fk_product": 3,  # 第三个产品
                "expected_result": "盘点单添加成功，显示成功提示"
            },
            {
                "case_desc": "添加成品仓库盘点",
                "ref": "INV-20250615-002",
                "title": "成品仓库盘点-2025年6月",
                "fk_warehouse": 3,  # 第三个仓库（成品仓）
                "fk_product": 5,  # 第五个产品
                "expected_result": "盘点单添加成功，显示成功提示"
            },
            {
                "case_desc": "添加配件仓库盘点",
                "ref": "INV-20250615-003",
                "title": "配件仓库月度盘点",
                "fk_warehouse": 4,  # 第四个仓库（配件仓）
                "fk_product": 2,  # 第二个产品
                "expected_result": "盘点单添加成功，显示成功提示"
            }
        ]
        for data in test_data:
            add_inventory(data)
            sleep(1)  # 等待操作完成
    except Exception as e:
        print("发生错误：", e)
    finally:
        sleep(1)
        browser.quit()